//
// Created by zlxx on 2022/10/20.
//

#ifndef MINIDB_SCAN_SELECTOR_H
#define MINIDB_SCAN_SELECTOR_H

#include <utility>

#include "sql/operator/operator.h"
#include "sql/expr/TupleValue.h"
#include "sql/expr/CompositeTuple.h"

class FilterStmt;
class Operator;
class Tuple;
class Table;
class ScanSelector {
public:
  ScanSelector(FilterStmt *filter_stmt, std::vector<Operator*> opers) : filter_stmt_(filter_stmt), opers_(std::move(opers))
  {}
  RC scan(std::vector<std::vector<TupleValue>>& tuple_sets);

  RC get_results(std::vector<std::vector<TupleValue>>& tuples,std::vector<CompositeTuple>& res);
private:
  FilterStmt *filter_stmt_;
  std::vector<Operator*> opers_;
  bool in_table_predicate(const TupleValue &tuple);
  RC descartes(std::vector<std::vector<TupleValue>>& tuples,int position,std::vector<CompositeTuple>& res,CompositeTuple& a_line);
  RC between_table_predict(const CompositeTuple &tuple,bool& ret) const;
  RC get_final_result(const std::vector<CompositeTuple>& tuples, std::vector<CompositeTuple> &res);
  void between_table_predict_in_recursive(const CompositeTuple &tuple, bool &ret) const ;


  bool compare_cell(const CompOp &comp, const TupleCell &left_cell, const TupleCell &right_cell) const;
};

#endif  // MINIDB_SCAN_SELECTOR_H
